package access

import (
	"errors"
	"server/common"
)

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
func getRouteServiceInfo(msg common.Message) (*common.ServiceInfo, error) {
	switch msg.Cmd {
	case
		common.LOGIN_REQ,
		common.LOGIN_ACK,
		common.LOGOUT_REQ,
		common.LOGOUT_ACK,
		common.CALL_MAKE_REQ,
		common.CALL_MAKE_ACK,
		common.CALL_END_REQ,
		common.CALL_END_ACK,
		common.P2P_START_PUBLISH_REQ,
		common.P2P_START_PUBLISH_ACK,
		common.P2P_STOP_PUBLISH_REQ,
		common.P2P_STOP_PUBLISH_ACK,
		common.P2P_START_SUBSCRIBE_REQ,
		common.P2P_START_PRODUCE_ACK,
		common.P2P_STOP_SUBSCRIBE_REQ,
		common.P2P_STOP_PRODUCE_ACK,
		common.P2P_CANDIDATE_NOTIFY,
		common.P2P_NEGOTIATE_REQ,
		common.P2P_NEGOTIATE_RES:
		return &common.ServiceInfo{ServiceType: common.SERVICE_TYPE_ONLINE}, nil
	case
		common.SFU_PREPARE_ROOM_REQ,
		common.SFU_JOIN_ROOM_REQ,
		common.SFU_LEAVE_ROOM_REQ,
		common.SFU_CREATE_TRANSPORT_REQ,
		common.SFU_CONNECT_TRANSPORT_REQ,
		common.SFU_START_PRODUCE_REQ,
		common.SFU_STOP_PRODUCE_REQ,
		common.SFU_START_PUBLISH_REQ,
		common.SFU_START_PUBLISH_ACK,
		common.SFU_STOP_PUBLISH_REQ,
		common.SFU_STOP_PUBLISH_ACK,
		common.SFU_START_SUBSCRIBE_REQ,
		common.SFU_STOP_SUBSCRIBE_REQ:
		return &common.ServiceInfo{ServiceType: common.SERVICE_TYPE_ROOM}, nil
	case
		common.REGISTER_SERVICE_REQ,
		common.CREATE_ROUTER_RES,
		common.CLOSE_ROUTER_RES,
		common.CREATE_TRANSPORT_RES,
		common.CONNECT_TRANSPORT_RES,
		common.CLOSE_TRANSPORT_RES,
		common.CREATE_PRODUCER_RES,
		common.CLOSE_PRODUCER_RES,
		common.CREATE_CONSUMER_RES,
		common.CLOSE_CONSUMER_RES:
		return &common.ServiceInfo{ServiceType: common.SERVICE_TYPE_ROOM}, nil
	default:
		return nil, errors.New("unknown message type: " + msg.Cmd)
	}
}

// -------------------------------------------------------------------------------------------------
//
// -------------------------------------------------------------------------------------------------
var responseCmdMap = map[string]string{
	common.LOGIN_REQ:                 common.LOGIN_RES,
	common.LOGOUT_REQ:                common.LOGOUT_RES,
	common.CALL_MAKE_REQ:             common.CALL_MAKE_ACK,
	common.CALL_END_REQ:              common.CALL_END_ACK,
	common.REGISTER_SERVICE_REQ:      common.REGISTER_SERVICE_RES,
	common.P2P_START_PUBLISH_REQ:     common.P2P_START_PUBLISH_RES,
	common.P2P_START_SUBSCRIBE_REQ:   common.P2P_START_SUBSCRIBE_RES,
	common.P2P_STOP_PUBLISH_REQ:      common.P2P_STOP_PUBLISH_RES,
	common.P2P_STOP_SUBSCRIBE_REQ:    common.P2P_STOP_SUBSCRIBE_RES,
	common.P2P_NEGOTIATE_REQ:         common.P2P_NEGOTIATE_RES,
	common.SFU_PREPARE_ROOM_REQ:      common.SFU_PREPARE_ROOM_RES,
	common.SFU_JOIN_ROOM_REQ:         common.SFU_JOIN_ROOM_RES,
	common.SFU_CREATE_TRANSPORT_REQ:  common.SFU_CREATE_TRANSPORT_RES,
	common.SFU_CONNECT_TRANSPORT_REQ: common.SFU_CONNECT_TRANSPORT_RES,
	common.SFU_START_PRODUCE_REQ:     common.SFU_START_PRODUCE_RES,
	common.SFU_START_PUBLISH_REQ:     common.SFU_START_PUBLISH_RES,
	common.SFU_START_SUBSCRIBE_REQ:   common.SFU_START_SUBSCRIBE_RES,
	common.SFU_STOP_PUBLISH_REQ:      common.SFU_STOP_PUBLISH_RES,
	common.SFU_STOP_SUBSCRIBE_REQ:    common.SFU_STOP_SUBSCRIBE_RES,
}

func getResponseCmd(cmd string) string {
	if res, ok := responseCmdMap[cmd]; ok {
		return res
	}
	return "UNKNOWN_CMD"
}
